<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="cloudLibrary.PMF" %>
<%@ page import="cloudLibrary.Item" %>
<%@ page import="cloudLibrary.Users" %>
<%@ page import="java.util.List" %>
<%@include file="aaa.jsp"%>

<%
     userService = UserServiceFactory.getUserService();
     user = userService.getCurrentUser();
%>
	<!--@include file="header.jsp"%>-->

<html>
	<head>
		<title>My Moviebook</title>
		<link rel="stylesheet" type="text/css" href="moviebook.css" />
	</head>
	<body>
		<div id='master'>
		
			<table id="header">
				<tr id='header-top'>
					<td colspan='5'></td>
				</tr>
				<tr>
					<td class='horizontal-border'>
						
					</td>
					<td id='logo'>
						<img src='img/logo.png' id='logo' />
					</td>
					
<%
    if (user != null) {
%>

					<td id='welcom-cell'>
						<span>You are log as <span id='nickname'><%= user.getNickname() %></span></span>
					</td>
					<td id='logout-cell'>
						<a href="<%= userService.createLogoutURL(request.getRequestURI()) %>">
							<img src='img/logout.png' id='logout' />
						</a>
					</td>

<%
    }
    else {
%>
					<td id='welcom-cell'>
					</td>
					<td id='logout-cell'>
					</td>
<%
    }
%>

					<td class='horizontal-border'>
						
					</td>
				</tr>
				<tr id='header-bottom'>
					<td colspan='5'></td>
				</tr>
			</table>
    
    
<%
if (user != null) {
     pageQuery = (String)request.getParameter("page");
    if (pageQuery == null)
		pageQuery = "myItems";
	 pm = PMF.get().getPersistenceManager();
%>
		<!-- @include file="createButtons.jsp" %> -->
        <%
	if (!pageQuery.equals("newFriend")) {
%>
<div class='button'>
	<a href="?page=newFriend">Add a new friend</a>
</div>
<%
	}
	if (!pageQuery.equals("newItem")) {
%>
		<div class='button'>
			<a href="?page=newItem">Create a new item</a>
		</div>
<%
	}
%>
        
		<!-- %@include file="msg.jsp" %>-->
        <%
String msg = (String)request.getParameter("msg");
String outputMsg = "";
if ((msg != null)) {
	if ((msg != null) && msg.equals("registered")) {
		outputMsg = "Your user has been registered successfully in the system !";
    }
    else if (msg.equals("requestsent")) {
    	outputMsg = "Your request has been sent to the owner!";
    }
    else if (msg.equals("lent")) {
		outputMsg = "The item " + (String)request.getParameter("title") + " was lent to " + (String)request.getParameter("friend") + " !";
	}
	else if (msg.equals("alreadylent")) {
		outputMsg = "The item " + (String)request.getParameter("title") + " is already lent by someone!";
	}
	else if (msg.equals("returned")) {
		outputMsg = "User " + (String)request.getParameter("user") + " has returned the item " + (String)request.getParameter("item");
	}
	else if (msg.equals("itemsaved")) {
		outputMsg = "This item was saved in the db !";
	}
%>
	<div style='clear:both; height:30px;'>
		<span style='color:#8888FF; font-size:15px; font-weight:bold;'>
			<%= outputMsg %>
		</span>
	</div>
<%
}
%>
		<!--%@include file="errMsg.jsp" %> -->
<%
	String error = (String)request.getParameter("error");
	String errMsg = "";
	if (error != null) {
		if (error.equals("fields")) {
			errMsg = "You need to fill in all fields.";
		}
		else if (error.equals("alreadyregistered")) {
			errMsg = "This email is already registered in the system.";
		}
		else if (error.equals("fields")) {
			errMsg = "You need to fill in all fields.";
		}
		else if (error.equals("alreadyin")) {
			errMsg = "This item is already added.";
		}
%>
		<div style='clear:both; height:30px;'>
			<span style='color:red; font-size:15px; font-weight:bold;'>
				<%= errMsg %>
			</span>
		</div>
<%
	}
%>


		<!--%@include file="tabMenu.jsp" %>-->
<div class='menu'>
	<div class='<%if (!pageQuery.equals("friendsItems") && !pageQuery.equals("friendsList") && !pageQuery.equals("newFriend")) {%>selected <%}%>tab'>
		<span style='font-size:16px;'>
			<a class='tab' href='?page=myItems'>
				My Items
			</a>
		</span>
	</div>
	<div class='<%if (pageQuery.equals("friendsItems")) {%>selected <%}%>tab'>
		<span style='font-size:16px;'>
			<a class='tab' href='?page=friendsItems'>
				Friends' Items
			</a>
		</span>
	</div>
	<div class='<%if (pageQuery.equals("friendsList") || pageQuery.equals("newFriend")) {%>selected <%}%>tab'>
		<span style='font-size:16px;'>
			<a class='tab' href='?page=friendsList'>
				Friends List
			</a>
		</span>
	</div>
	<div style='clear:both;'></div>
</div>
        
        
<%
		if ((pageQuery != null) && pageQuery.equals("friendsList")) {
%>
			<!--%@include file="userList.jsp" %>-->
            <%
	String userQuery = "select from " + Users.class.getName() + " where email!='"+ user.getEmail() +"'";
    List<Users> users = (List<Users>) pm.newQuery(userQuery).execute();
%>
<table class='list'>
	<% 
	int alternate = 0;
	for (Users u : users) { 
		++alternate;
	%>
	<tr class='<% if(alternate%2==0){%>item-odd<%}else{%>item-pair<%}%>'
		onmouseover='this.className="item-over";'
		onmouseout='this.className="<% if(alternate%2==0){%>item-odd<%}else{%>item-pair<%}%>";'>
    	<td class='friend-name'>
    		<%= u.getName()%>
        </td>
        <td>
    		<%= u.getEmail()%>
        </td>
        <td class='friend-num'>
    		<%= u.getTel()%>
        </td>
    </tr>
<%
    }
%>
</table>
<%
		} else if ((pageQuery != null) && pageQuery.equals("friendsItems")) {
%>
			<!--%@include file="friendCollection.jsp" %>-->
<%    
    String query = "select from " + Item.class.getName() +  " where owner!='"+ user.getEmail() +"'";
    List<Item> items = (List<Item>) pm.newQuery(query).execute();

    if (items.isEmpty()) {
%>
	<p>Your friends have no items for borrowing.</p>
<%
    } else {
%>		
<table class='list'>
<%
		int alternate = 0;
        for (Item i : items) {
			++alternate;
%> 
					<tr class='<% if(alternate%2==0){%>item-odd<%}else{%>item-pair<%}%>'
						onmouseover='this.className="item-over";'
						onmouseout='this.className="<% if(alternate%2==0){%>item-odd<%}else{%>item-pair<%}%>";'>
						<td class='item-title'>
							<%= i.getTitle() %>
						</td>
						<td class='item-ico'>
							<img class='ico' src='
							<% if (i.getType().equals("DVD")) {%>
							img/dvd.png
							<%} else if(i.getType().equals("CD")) {%>
							img/cd.png
							<%} else if(i.getType().equals("Book")) {%>
							img/book.png
							<%}%>
							' />
						</td>
						<td class='item-type'>
							<%= i.getType() %>
						</td>
						<td class='item-empty'>
						</td>
					    <td class='item-request'>
					       <form action="/borrowItem" method="post" >
					    	<input type="hidden" value="<%= i.getItemID().getId() %>" name="itemID" />
					        <input class='request' type="submit" value="Request Borrowing" <% if(i.isReserved()){ %> disabled="disabled" <% } %>/>
					       </form>
					    </td>
					</tr>        

<%
        }
    }
    pm.close();
%>
</table>
<%
		} else if ((pageQuery != null) && pageQuery.equals("newItem")) {
%>
			<!--%@include file="addItem.jsp" %>-->
            
<div class='pageSpace'>
	<div class='addNew'>
		<h2>CREATE NEW ITEM : </h2>
			<form action="/createItem" method="post">
			<table class='addNew'>
		    <tr>
			    <td class='lbl'>
			    	Owner: 
			    </td>
			    <td>
			    	<input class='addNew' type="text"  value="<%= user.getEmail() %>" disabled="disabled" />
			    </td>
		    </tr>
		    <tr>
			    <td class='lbl'>
			   	 	Title: 
			    </td>
			    <td>
			    	<input class='addNew' type="text"  name="title" />
			    </td>
		    </tr>
		    <tr>
			    <td class='lbl'>
			    	Type:
			    </td>
			    <td>
			    	<select class='addNew' name="type">
			    			<option> DVD</option>
			                <option> CD </option>
			                <option> Book </option>
			    	</select>
			    </td>
		    </tr>
		    <tr class='submitNew'>
		    	<td class='submitNew' colspan='2'>
			    	<input class='request' type="submit" value="Create" />
			    </td>
		    </tr>
		   	</table>
		</form>
	</div>
</div>
            
<%
		} else if ((pageQuery != null) && pageQuery.equals("newFriend")) {
%>
			<!--%@include file="addUser.jsp" %>-->
<div class='pageSpace'>
	<div class='addNew'>
		<h2>ADD A NEW FRIEND : </h2>
		<form action="/register" method="post">
			<table class='addNew'>
		    <tr>
			    <td class='lbl'>Email: </td>
			    <td><input class='addNew' type="text"  name="email" />
			</tr>
		    <tr>
			    <td  class='lbl'>Name: </td>
			    <td><input class='addNew' type="text"  name="name" />
			</tr>
		    <tr>
		    	<td class='lbl'>Tel: </td>
		    	<td><input class='addNew' type="text"  name="tel" />
		    </tr>
		    <tr class='submitNew'>
		    	<td class='submitNew' colspan='2'>
					<input class='request' type="submit" value="ADD" />
				</td>
		    </tr>
		    </table>
		</form>
	</div>
</div>
            
            
            
<%
		} else {
%>
			<!--%@include file="myCollection.jsp" %>-->
            <%
	String userQuery = "select from " + Users.class.getName() + " where email!='"+ user.getEmail() +"'";
    List<Users> users = (List<Users>) pm.newQuery(userQuery).execute();
    String query = "select from " + Item.class.getName() + " where owner=='"+ user.getEmail() +"'";
    List<Item> items = (List<Item>) pm.newQuery(query).execute();
    
	if (items.isEmpty()) {
%>
		<p>Your db has no items.</p>
<%
    } else {
%>

				<table class='list'>
<%
		int alternate = 0;
        for (Item i : items) {
			++alternate;
%>
					<tr class='<% if(alternate%2==0){%>item-odd<%}else{%>item-pair<%}%>'
						onmouseover='this.className="item-over";'
						onmouseout='this.className="<% if(alternate%2==0){%>item-odd<%}else{%>item-pair<%}%>";'>
						<td class='item-title'>
							<%= i.getTitle() %>
						</td>
						<td class='item-ico'>
							<img class='ico' src='
							<% if (i.getType().equals("DVD")) {%>
							img/dvd.png
							<%} else if(i.getType().equals("CD")) {%>
							img/cd.png
							<%} else if(i.getType().equals("Book")) {%>
							img/book.png
							<%}%>
							' />
						</td>
						<td class='item-type'>
							<%= i.getType() %>
						</td>
						<td class='item-lend'>
<%
						if (i.isReserved() ){
%>
						<span style='font-size:12px; color:red; font-weight:bold;'>Lent to </span>
						<span style='font-size:13px; color:#AAAAAA; font-weight:bold; margin-left:20px;'><%=i.getBorrower() %></span>
<%
							 } 
							 else {
%>
						       <form action="/lendItem" method="post" >
							    	<input type="hidden" value="<%= i.getItemID().getId() %>" name="itemID" />
							        <select class='friend' name="person">
							        		<% for (Users u : users){%>
							    			   <option> <%= u.getEmail() %> </option>
							                <% }%>
							    		  </select>
							        <input class='lend' type="submit" value="Lend to" />
						       </form>
<%
						      }
%>
						</td>
						<td class='item-return'>
						   	   <form action="/deleteItem" method="post" >
							    	<input type="hidden" value="<%= i.getItemID().getId() %>" name="itemID" />
							        <input type="hidden" value="<%= user.getEmail() %>" name="person" />
							        <input class='return' type="submit" value="Return" <% if(!i.isReserved()){ %> disabled="disabled" <% } %>/>
						       </form>
						</td>
					</tr>
<%
        }
%>
				</table>


<%
    }
%>
<%
		}
} else {
%>
	<!--%@include file="login.jsp" %> -->
<br />
<br />
<span style='font-weight:bold; font-size:14px;'>
	Welcome to our Cloud Project.
</span><br />
<br />
<span style='font-size:14px;'>
	<a style='color:red;' href="<%= userService.createLoginURL(request.getRequestURI()) %>">
		Sign in
	</a>
	to include your name with greetings you post.
</span><br />
<br />
<%
}
%>
	<!--%@include file="footer.jsp" %> -->
    
		</div>
		<div id='footer'>
			Developed by G., L. & C. - Powered by Google Apps
		</div>
	</body>
</html>



